<template>
  <div class="permission">
    <el-card>
      <div style="text-align: right">
        <el-button type="primary" @click="addclick(1, '0')">添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              <el-button
                type="text"
                @click="addclick(1, row.id)"
              >添加页面</el-button>
              <el-button
                type="text"
                @click="addclick(2, row.id)"
              >添加按钮</el-button>
              <el-button
                type="text"
                @click="editCilck(row)"
              >编辑权限点</el-button>
              <el-button type="text" @click="delClick(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增权限 -->
    <Add ref="addform" @getData="getData" />
  </div>
</template>
<script>
import { sysPermission, sysPermissionDel } from '@/api/permission'
import { changeData } from '@/utils/index'
import Add from './components/add.vue'
export default {
  name: '',
  components: { Add },
  data() {
    return {
      list: []
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysPermission()
      console.log(res, 222)
      this.list = changeData(res.data, '0')
    },
    // 删除权限点
    delClick(row) {
      this.$confirm('确定要删除该权限点吗', '提示')
        .then(async() => {
          await sysPermissionDel(row.id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },
    addclick(type, pid) {
      this.$refs.addform.isShow = true
      // 修改tpye和pid
      this.$refs.addform.form.type = type
      this.$refs.addform.form.pid = pid
      this.$refs.addform.mode = 'add'
    },
    editCilck(row) {
      this.$refs.addform.isShow = true
      this.$refs.addform.form = JSON.parse(JSON.stringify(row))
      this.$refs.addform.mode = 'edit'
    }
  }
}
</script>
<style lang="scss" scoped>
.permission {
  padding: 15px;
}
</style>
